package org.huqiwen.blog.logic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.huqiwen.blog.dao.DataSource;
import org.huqiwen.blog.dao.ExecuteOptionSQL;
import org.huqiwen.blog.data.CommentDataBean;
/**
 * 此类为评论的处理类
 * @author Administrator
 *
 */
public class CommentLogicBean {
	private int allRows;
	private int curPage = 1;
	private int allPage;
	private int pageSize = 10;
	
	public int getAllRows() {
		return allRows;
	}

	public void setAllRows(int allRows) {
		this.allRows = allRows;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getAllPage() {
		return allPage;
	}

	public void setAllPage(int allPage) {
		this.allPage = allPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/**
	 * 通过文章ID,返加此文章的评论
	 * @param id 文章id
	 * @return 此文章下面的评论列表
	 */
	public List<CommentDataBean> findAllComment(String id){
		List<CommentDataBean> list = new ArrayList<CommentDataBean>();
		Statement stmt = null;
		ResultSet rs = null;
		try{
			String sql = "select * from comments where comment_post_id="+id;
			stmt = DataSource.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
			rs.last();
			this.allRows = rs.getRow();
			if (this.allRows%this.pageSize == 0) {
				this.allPage = this.allRows/this.pageSize;
			}else {
				this.allPage = this.allRows/this.pageSize + 1;
			}
			
			if (this.curPage ==1) {
				rs.beforeFirst();	//如果是第一页，将指针移动到记录的第一列
			}else {
				rs.absolute((this.curPage-1)*this.pageSize);//否则移动到当前页的第一条记录
			}
			int i = 0;
			while(rs.next()&&i<this.pageSize){
				CommentDataBean bean = new CommentDataBean();
				bean.setCommentId(rs.getString(1));
				bean.setCommentPostId(rs.getString(2));
				bean.setCommentAuthor(rs.getString(3));
				bean.setCommentAuthorEmail(rs.getString(4));
				bean.setCommentAuthorUrl(rs.getString(5));
				bean.setCommentAuthorIp(rs.getString(6));
				bean.setCommentDate(rs.getString(7));
				bean.setCommentContent(rs.getString(8));
				list.add(bean);
				i++;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try {
				if(stmt!=null) rs.close();
				if(rs!=null) rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}			
		}
		return list;
	}
	/**
	 * 此方法根据传入的参数，进行文章添加的操作
	 * @param commentPostId
	 * @param commentAuthor
	 * @param commentEmail
	 * @param commentUrl
	 * @param commentIp
	 * @param commentContent
	 * @return
	 */
	public int addComment(String commentPostId,String commentAuthor,String commentEmail,String commentUrl,String commentIp,String commentContent){
		return  ExecuteOptionSQL.UpdateSQL("insert into comments(comment_post_id,comment_author,comment_author_email,comment_author_url,comment_author_ip,comment_date,comment_content)values('"
				+commentPostId+"','"+commentAuthor+"','"+commentEmail+"','"+commentUrl+"','"+commentIp+"',now(),"+"'"+commentContent+"')");
	}
	/**
	 * 根据传进来的评论ID,删除相应的评论
	 * @param id
	 * @return
	 */
	public int delComment(String id){
		return ExecuteOptionSQL.UpdateSQL("delete from comments where comment_id ="+id);
	}
}
